<%@tag description="Table tag" pageEncoding="UTF-8"%>
<%@ include file="/taglibs.jsp" %>
<%@tag import="pkg.dao.PagedList" %>
<%@tag import="java.util.Collection" %>

<%-- The list of normal or fragment attributes can be specified here: --%>
<%@attribute name="items" required="true" type="java.lang.Object"%>
<%@attribute name="var" required="true" type="java.lang.String" rtexprvalue="false"%>
<%@attribute name="id" required="false"%>
<%@attribute name="css_class" required="false"%>
<%@attribute name="pagesize" type="java.lang.Integer" required="false"%>
<%@attribute name="page" type="java.lang.Integer" required="false"%>
<%@attribute name="range" type="java.lang.Integer" required="false"%>


<%@variable alias="idx" name-from-attribute="var" variable-class="java.lang.Object"%>
<%
            jspContext.setAttribute("_colNames", new java.util.ArrayList(), PageContext.REQUEST_SCOPE);
            int total = 0;
            Collection rowItems = null;
            if (items instanceof PagedList) {
                PagedList pl = (PagedList)items;
                total = pl.getFullListSize();
                rowItems = pl.getList();
                pagesize = pl.getObjectsPerPage();
            } else if (items instanceof Collection) {
                Collection c = (Collection)items;
                total = c.size();
                rowItems = c;
            }
            if (pagesize == null) {
                pagesize = total;
            }
            if (range == null) {
                range = 5;
            }
            int pages = total / pagesize;
            if (total % pagesize != 0) {
                pages = pages + 1;
            }
ServletContext sc;
sc.getInitParameter(name)
            page = Math.max(1, Math.min(pages, page));

            int dp = range / 2;
            int startPage = Math.max(1, page - dp);
            int endPage = Math.min(startPage + range - 1, pages);
            if (endPage - startPage < range) {
                startPage = Math.max(1, endPage - (range - 1));
            }


%>
<c:if test="${!empty id}">
    <c:set var="idclause" value="id=\"${id}\""/>
</c:if>
<c:set var="classclause" value="class=\"datatable\""/>
<c:if test="${!empty css_class}">
    <c:set var="classclause" value="class=\"${css_class}\""/>
</c:if>
<table ${classclause} ${idclause}>
    <c:set var="_colsSet" value="false" scope="request"/>
    <c:forEach items="${items}" var="v" varStatus="status">
        <c:set var="idx" value="${v}"/>
        <c:set var="_colValue" value="${v}" scope="request"/>
        <jsp:doBody var="bodyText"/>
        <c:set var="_colsSet" value="true" scope="request"/>
        <c:if test="${status.first}">
            <c:forEach items="${_colNames}" var="colName">
                <th>${colName}</th>
            </c:forEach>
        </c:if>
        <c:if test="${status.index % 2 == 0}">
            <c:set var="oe" value="odd"/>
        </c:if>
        <c:if test="${status.index % 2 == 1}">
            <c:set var="oe" value="even"/>
        </c:if>
        <tr class="${oe}">${bodyText}</tr>
    </c:forEach>
</table>